<template>
  <div class="setting">
    <!-- card -->
    <el-card>
      <!-- tabs -->
      <el-tabs v-model="activeName">
        <el-tab-pane label="角色管理" name="first">
          <!-- 1 - 按钮 -->
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-plus"
            @click="isShow = true"
          >新增角色</el-button>
          <!-- 1 - table -->
          <el-table
            :data="tableData"
            style="width: 100%"
            :default-sort="{ prop: 'date', order: 'descending' }"
            border
          >
            <el-table-column type="index" label="序号" sortable />
            <el-table-column prop="name" label="角色名" sortable />
            <el-table-column prop="description" label="描述" />
            <el-table-column fixed="right" label="操作">
              <!-- 通过 slot自定义结构  如果要获取到这一行的数据 可以通过 .row属性来获取 -->
              <template #default="{ row }">
                <el-button
                  type="text"
                  size="small"
                  @click="handleClick(row)"
                >分配权限</el-button>
                <el-button
                  type="text"
                  size="small"
                  @click="enterEdit(row)"
                >修改</el-button>
                <el-popconfirm
                  title="这是一段内容确定删除吗？"
                  @onConfirm="onConfirm(row)"
                >
                  <el-button
                    slot="reference"
                    type="text"
                    size="small"
                  >删除</el-button>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
          <!-- 1 - 分页 -->
          <el-pagination
            :current-page="currentPage4"
            :page-sizes="[1, 2, 3, 4, 5]"
            :page-size="pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-tab-pane>
        <el-tab-pane label="公司信息" name="second">
          <!-- 公司信息 -->
          <company-info />
        </el-tab-pane>
      </el-tabs>

      <set-dialog
        :visible="isShow"
        :form="form"
        @update:visible="isShow = $event"
        @getDate="getDate"
        @clear="clearHandler"
      />
    </el-card>
  </div>
</template>

<script>
import companyInfo from './components/companyInfo.vue'

import { getRoles, getRoleById, deleteRoleById } from '@/api/setting'
import SetDialog from './components/SetDialog.vue'
export default {
  name: 'Setting',
  components: { companyInfo, SetDialog },
  data() {
    return {
      activeName: 'first',
      currentPage4: 1,
      pagesize: 4,
      tableData: [],
      total: 0,
      page: 1,
      isShow: false,
      // 表单数据
      form: {
        name: '',
        description: ''
      }
    }
  },
  async created() {
    this.getDate()
  },
  methods: {
    // 添加删除事件
    async onConfirm(row) {
      await deleteRoleById(row.id)
      // console.log(row)
      if (this.tableData.length === 1 && this.page != 1) {
        this.page--
      }
      this.$message.success('删除成功')
      this.getDate()
    },

    async enterEdit(row) {
      // console.log('row:', row)
      const res = await getRoleById(row.id)
      // console.log('res:', res)
      // 设置给form
      this.form = res
      // 弹出dialog
      this.isShow = true
    },
    // 清空数据
    clearHandler() {
      this.form = {
        name: '',
        description: ''
      }
    },
    // 获取数据
    async getDate() {
      const res = await getRoles({ page: this.page, pagesize: this.pagesize })
      // console.log(res)
      this.total = res.total
      this.tableData = res.rows
    },
    // table内部点击
    handleClick(row) {
      console.log('row:', row)
    },
    // 页容量改变
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.getDate()
    },
    // 页码改变
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.page = val
      this.getDate()
    }
  }
}
</script>

<style lang="scss">
.setting {
  .el-form-item__content {
    width: 600px;
  }
  .el-pagination {
    text-align: right;
  }
}
</style>
